package com.haha.gulimall.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.product.dao.SkuInfoDao;
import com.haha.gulimall.product.entity.SkuInfoEntity;
import com.haha.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * key: adf
     * catelogId: 225
     * brandId: 23
     * min: 100
     * max: 60
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageCondion(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> qw = new QueryWrapper<>();
        String key = (String) params.get("key");
        qw.and(StringUtils.isNotEmpty(key),wrapper->{
            wrapper.or().eq("sku_id",key)
                    .or().like("sku_name",key)
                    .or().like("sku_desc",key);
        });
        String brandId = (String) params.get("brandId");
        String catelogId = (String) params.get("catelogId");
        qw.eq(StringUtils.isNotEmpty(brandId)&& !"0".equals(brandId),"brand_id",brandId)
                .eq(StringUtils.isNotEmpty(catelogId)&&!"0".equals(catelogId),"catalog_id",catelogId)
                .ge(StringUtils.isNotEmpty((String) params.get("min")),"price",(String) params.get("min"))
                .le((StringUtils.isNotEmpty((String) params.get("max")) && new BigDecimal((String) params.get("max")).signum()==1),"price",(String) params.get("max"));
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),qw

        );

        return new PageUtils(page);
    }

}